package com.turnengine.client.global.game.command;

import com.javabi.command.IExecutableCommand;
import com.javabi.command.IExecutableCommandResponse;
import com.javabi.command.serializer.AbstractCommandDataSerializer;
import com.javabi.common.io.data.IDataReader;
import com.javabi.common.io.data.IDataWriter;
import com.turnengine.client.global.game.bean.GameSerializer;
import com.turnengine.client.global.game.bean.IGame;
import java.io.IOException;

/**
 * The New Game Data Serializer.
 */
public class NewGameDataSerializer extends AbstractCommandDataSerializer<IGame> implements INewGameDataSerializer {

	/**
	 * Creates a new New Game Data Serializer.
	 */
	public NewGameDataSerializer() {
		super(NewGame.COMMAND_ID);
	}

	@Override
	public IGame readReturnValue(IDataReader reader) throws IOException {
		IGame value = new GameSerializer().readObject(reader);
		return value;
	}

	@Override
	public void writeReturnValue(IDataWriter writer, IGame value) throws IOException {
		new GameSerializer().writeObject(writer, value);
	}

	@Override
	public IExecutableCommand<IGame> readRequest(IDataReader reader) throws IOException {

		// Read fields
		long loginId = reader.readLong();
		String name = reader.readString(true);

		// Create object
		NewGame object = new NewGame();
		object.setLoginId(loginId);
		object.setName(name);
		return object;
	}

	@Override
	public void writeRequest(IDataWriter writer, IExecutableCommand<IGame> argument) throws IOException {
		NewGame object = (NewGame) argument;

		// Get fields
		long loginId = object.getLoginId();
		String name = object.getName();

		// Write fields
		writer.writeLong(loginId);
		writer.writeString(name, true);
	}

	@Override
	public IExecutableCommandResponse<IGame> newResponse() {
		return new NewGameResponse();
	}
}
